package com.aaa.controller.card;

import com.aaa.entity.Card;
import com.aaa.service.CardService;
import com.aaa.service.impl.CardServiceImpl;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ChangeCardStatusServlet")
public class ChangeCardStatusServlet extends HttpServlet {
    CardService cardService = null;

    @Override
    public void init() throws ServletException {
        cardService = new CardServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer cardId = IntegerUtils.ToInteger(req.getParameter("cardId"));
        Integer status = IntegerUtils.ToInteger(req.getParameter("status"));

        Card card = new Card();
        if(status==1){
            card.setStatus(0);
        }else{
            card.setStatus(1);
        }
        card.setCardId(cardId);

        int i = cardService.changeStatus(card);
        resp.getWriter().print(new Gson().toJson(card));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
